package com.winderinfo.yikaotianxia.ui.mockexam;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.winderinfo.yikaotianxia.R;
import com.winderinfo.yikaotianxia.core.api.Api;
import com.winderinfo.yikaotianxia.core.base.BaseActivity;
import com.winderinfo.yikaotianxia.core.mvp.Contract;
import com.winderinfo.yikaotianxia.core.mvp.Presenter;
import com.winderinfo.yikaotianxia.core.utils.LogUtil;
import com.winderinfo.yikaotianxia.core.utils.MapUtil;
import com.winderinfo.yikaotianxia.core.utils.SPUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class AnswerActivity extends BaseActivity implements Contract.View {
    @BindView(R.id.sin_pager)
    ViewPager sinPager;
    @BindView(R.id.sin_time)
    TextView sinTime;
    @BindView(R.id.top)
    RelativeLayout top;
    @BindView(R.id.bot)
    RelativeLayout bot;

    private Presenter presenter;
    private Map<String, Object> map = new HashMap<>();
    private SPUtils spUtils;

    private int id;
    private String userID;
    private int cardid;

    @Override
    protected int initLayoutId() {
        return R.layout.activity_singleelection;
    }

    @Override
    protected void initView() {
        //实例P
        presenter = new Presenter(this);
        spUtils = new SPUtils(this);
        Intent intent = getIntent();
        id = intent.getIntExtra("id", 0);
        //获取用户Id
        userID = spUtils.getString("UserID", "");
        MapUtil.cardBeanMap = new HashMap<>();

    }

    @Override
    protected void initData() {
        //请求答题卡
        /**
         * @userId 用户id
         * @realTitlesId 真题id
         * */
        map.clear();
        map.put("userId", 1);
        map.put("realTitlesId", id);
        //请求真题详情
        presenter.onPostRequest(Api.ANSWERCARD, map, AnswerCardBean.class);
    }

    @OnClick({R.id.sin_back, R.id.sin_collection, R.id.sin_answercard})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.sin_back:
                finish();
                break;
            case R.id.sin_collection:
                break;
            case R.id.sin_answercard:
                break;
        }
    }

    @Override
    public void onSuccess(Object data) {
        //答题卡
        if (data instanceof AnswerCardBean) {
            //答题卡id
            cardid = ((AnswerCardBean) data).getYkScore().getId();
            LogUtil.i("cardid", cardid + "");
            /**
             * @id 真题id
             * */
            map.clear();
            map.put("id", id);
            presenter.onPostRequest(Api.ANSWER, map, AnswerBean.class);
        }
        //详情数据
        if (data instanceof AnswerBean) {
            List<AnswerBean.RowsBean> rows = ((AnswerBean) data).getRows();
            AnswerFAdapter answerFAdapter = new AnswerFAdapter(getSupportFragmentManager(), rows, cardid, (AnswerBean) data, id);
            sinPager.setAdapter(answerFAdapter);
        }
    }

    @Override
    public void onFailure(Object error) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        presenter.onUntying();
    }

}
